package com.magnificent.atlantis.client.robot.ui.configure.table;

import com.magnificent.atlantis.client.robot.strategies.RobotManagerStrategy;
import com.magnificent.atlantis.client.robot.strategies.RobotStrategy;
import com.magnificent.atlantis.client.robot.strategies.RobotWorkerStrategy;
import com.magnificent.atlantis.client.robot.ui.configure.RobotConfiguration;
import com.magnificent.atlantis.common.entity.ServerUser;
import com.magnificent.atlantis.common.user.ServerDescriptor;
import com.magnificent.commons.utils.collections.list.MetaArrayList;

import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 * User: Babanin
 * Date: Aug 29, 2009
 * Time: 2:32:34 AM
 */
public class RobotConfigurationTableModel extends DefaultTableModel {

    public static String[] COLUMN_NAMES = new String[]{"Name", "Strategy", "Plan period", "Work day start", "Work day end", "Activated"};

    private List<RobotConfiguration> robotConfigurations;

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public int getRowCount() {
        return getRobotConfigurations().size();
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 1) {
            return String[].class;
        }
        return super.getColumnClass(columnIndex);
    }

    @Override
    public Object getValueAt(int row, int column) {
        RobotConfiguration configuration = robotConfigurations.get(row);

        switch (column) {
            case 0:
                return configuration.getUser().getLogin();
            case 1:
                return configuration.getStrategy();
            case 2:
                return configuration.getDayPeriod();
            case 3:
                return configuration.getWorkDayStart();
            case 4:
                return configuration.getWorkDayFinish();
            case 5:
                return configuration.isActivated();
        }

        throw new IllegalStateException("Program internal error");
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        super.setValueAt(aValue, row, column);

        RobotConfiguration configuration = robotConfigurations.get(row);

        switch (column) {
            case 1:
                configuration.setStrategy((RobotStrategy) aValue);
                break;
            case 2:
                configuration.setDayPeriod((Integer) aValue);
                break;
            case 3:
                configuration.setWorkDayStart((Integer) aValue);
                break;
            case 4:
                configuration.setWorkDayFinish((Integer) aValue);
                break;
            case 5:
                configuration.setActivated((Boolean) aValue);
                break;
        }
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return column >= 1;
    }

    public List<RobotConfiguration> getRobotConfigurations() {
        if (robotConfigurations == null) {
            robotConfigurations = new MetaArrayList<RobotConfiguration>();

            ServerDescriptor descriptor = ServerDescriptor.load("dataClient/server.asd");
            for (ServerUser user : descriptor.getUserList()) {
                RobotConfiguration robotConfiguration = new RobotConfiguration();

                robotConfiguration.setUser(user);
                robotConfiguration.setActivated(true);
                robotConfiguration.setStrategy(user.getLogin().contains("manager") ?
                        new RobotManagerStrategy(null) : new RobotWorkerStrategy(null));

                robotConfiguration.setOtherUsers(new MetaArrayList<ServerUser>(descriptor.getUserList()));

                robotConfiguration.setHost(descriptor.getHost());
                robotConfiguration.setPort(descriptor.getPort());

                System.out.println("User loaded: " + robotConfiguration.toString());
                robotConfigurations.add(robotConfiguration);
            }
        }

        return robotConfigurations;
    }
}
